require('../common/header/index');
require('../common/sidebar/index');
var _util = require('../../util/util')
const vue = require('../common/vue');
const _evaluation_service = require('../../service/evaluation_service');
let _cart_service = require('../../service/cart-service');
var _product_service = require('../../service/product-service')
let _favourite_vm = new vue({
    // el: '#favourite-products',
    el: '#favourite-items',
    data(){  
        return {
            pagesize: 6,
            productList: [],
            current: 1,
            total: 0,
            pagesequence: [],
            pages: 0
        };
    },
    methods: {
        fetchProducts: function(_this) {
            _evaluation_service.getFavouriteProducts(_this.pagesize, _this.current, function(res) {
                _this.productList = res.records;
                _this.pagesequence = [];
                _this.currentpage = res.current;
                _this.pages = res.pages;
                for(let i=1;i<=res.pages;i++){
                    _this.pagesequence[i-1] = i;
                }
                for(let item of _this.productList){
                    var pair = item.src.split("\"");
                    item.src = "../image/" + pair[1].substring(7);
                }
                _this.current = res.current;
                _this.total = res.total;
                console.log("success");
                console.log(_this.productList);
            }, function(err) {
                console.log("error");
                console.log(err);
            })
        },
        backpage: function (event) {
            if (this.current > 1) {
                this.current = this.current - 1;
                this.fetchProducts(this);
            }
        },
        nextpage: function (event) {
            if (this.current + 1 <= this.pages) {
                this.current = this.current + 1;
                this.fetchProducts(this);
            }
        },
        toPage: function(pagenum) {
            this.current = pagenum;
            this.fetchProducts(this);
        },
        toBlockView: function() {
            $("#listSpan").removeClass("btn-primary");
            $("#blockSpan").addClass("btn-primary");
        },
        toListView: function() {
            $("#blockSpan").removeClass("btn-primary");
            $("#listSpan").addClass("btn-primary");
        },
        addToCart: function(itemid) {
            _product_service.getOneItem(itemid, function(res) {
                let attrnum = res.length;
                let attrdescription = "";
                let price = res[0].price * res[0].specialrate;
                for (let i = 0; i < attrnum - 1; i++) {
                    attrdescription = attrdescription + " " + res[i].attrList[0].attrvalue;
                    price = price * res[i].attrList[0].rate;
                }
                attrdescription = attrdescription + " 单位" + res[attrnum-1].attrList[0].attrvalue;
                let content = {
                    num: 1,
                    price: price,
                    attrdescription: attrdescription
                }
                _cart_service.addToCart(itemid, function(res) {
                    window.location.href = "/view/product_summary.html";
                }, function(err) {
                    console.log(err);
                }, content);
            }, function(err) {
                console.log(err);
            });
        },
    },
    created: function () {
        this.fetchProducts(this);
    }
})